#Binary parser template

import os, sys
from Sanae3D.lib.StructReader import StructReader
from Sanae3D.lib.ModelObject import Model3D

class DragonNest_MSH(Model3D):
        
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(DragonNest_MSH,self).__init__("MSH")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def read_name(self, n):
        
        return self.inFile.read_string(n).split("\x00")[0]
    
    def parse_faces(self, numIndex, meshName):
        '''Each index references the vertex, normal, and texture values'''
        
        print self.inFile.tell(), "faces", numIndex
        info = self.inFile.read_short(numIndex)
            
    def parse_vertices(self, numVerts, meshName):

        print self.inFile.tell()
        for i in range(numVerts):
            vx, vy, vz = self.inFile.read_float(3)
            
            vx = float(vx) * -1
            self.create_vertex(meshName, i)                        
            self.add_coords(meshName, i, [vx, vy, vz])

        print self.inFile.tell()
        for i in range(numVerts):
            nx, ny, nz = self.inFile.read_float(3)
            self.add_vert_normals(meshName, i, [nx, ny, nz])
       
        for i in range(numVerts):
            tu, tv = self.inFile.read_float(2)
            self.add_vert_uv(meshName, i, [tu, tv])
        
        for i in range(numVerts):
            b1, b2, b3, b4 = self.inFile.read_short(4)

        for i in range(numVerts):
            w1, w2, w3, w4 = self.inFile.read_float(4)

        numBones = self.inFile.read_long()
        print numBones
        for i in range(numBones):
            print self.read_name(256)
            
        
    def parse_mesh(self, numMesh):
        
        print "%d meshes" %numMesh
        for i in range(numMesh):
            sceneName = self.read_name(256)
            meshName = self.read_name(256)
            numVerts, numIndex, unk1, renderMode = self.inFile.read_long(4)
            self.inFile.seek(496, 1)
            
            print sceneName, meshName, numVerts, numIndex
            self.create_object(meshName)
            
            self.parse_faces(numIndex, meshName)
            self.parse_vertices(numVerts, meshName)
        
    def parse_bones(self, numBones):
        
        print "%d bones" %numBones
        for i in range(numBones):
            boneName = self.read_name(256)
            boneMatrix = self.inFile.read_float(16)
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        magic = self.read_name(256)
        version, numMesh, unk1, unk2 = self.inFile.read_long(4)
        bbMax = self.inFile.read_float(3)
        bbMin = self.inFile.read_float(3)
        numBones, unk3, numOther = self.inFile.read_long(3)
        
        self.inFile.seek(1024)
        self.parse_bones(numBones)
        self.parse_mesh(numMesh)
        
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = DragonNest_MSH(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "Eterni", "msh", "Dragon Nest MSH"

if __name__ == '__main__':
    
    file1 = "ac_academicglove_minotaours.msh"
    obj = read_file(file1)